using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class ManagerController :UI.BaseController
    {
        public const string ProgrameCode = "Manager";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public ManagerController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.Manager), "UserName,RealName,Group.GroupName");
            string orderby = null;
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;
            List<object> ps = new List<object>();
            if (BLL.LoginUser.LoginManager.ManagerType != Common.EnumType.ManagerType.Super)
            {
                if (string.IsNullOrEmpty(where))
                {
                    where = "ManagerType=?"; 
                }
                else
                {
                    where += " and ManagerType=?";
                }
                ps.Add(BLL.LoginUser.LoginManager.ManagerType);
            }
            var bll = new BLL.Manager();
            var list = bll.Get(where, orderby, (page - 1) * rows, rows, ps.ToArray());
            List<object> listView = new List<object>();

            foreach (var i in list)
            {
                listView.Add(new
                {
                    UserName = i.UserName,
                    RealName = i.RealName,
                    GroupName = i.GroupID == null ? null : i.Group.GroupName,
                    Email = i.Email,
                    IsLock = i.IsLock,
                    ManagerType=i.ManagerType,
                    RoleCount = i.ManagerRoles.Count,
                    LockFailTime = UI.Utils.FormateDate(i.LockFailTime),
                    CreatTime = UI.Utils.FormateDate(i.CreatTime),
                    Onlines = BLL.LoginUser.OnLineManagers.Where(r=>r.IsKick==false&&r.UserName==i.UserName).Count()
                });
            }
            var ret = new
            {
                total=bll.GetCount(where,ps.ToArray()),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]
        public string Delete(string id)
        {
            var ret = new BLL.Manager().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "add")]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]
        public string Add(Model.Manager model)
        {
            model.UserName = model.UserName.ToUpper();
            if (BLL.LoginUser.LoginManager.ManagerType != Common.EnumType.ManagerType.Super) model.ManagerType = BLL.LoginUser.LoginManager.ManagerType;
            var ret=new BLL.Manager().Add(model);
            return UI.Utils.ToJsonString(ret);
            
        }

        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public ActionResult Edit(string  UserName)
        {
            var model = new BLL.Manager().Get(UserName);
            if (model == null) return UI.Utils.ErrorDateNoExist();
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public string Edit(Model.Manager model)
        {
            if (BLL.LoginUser.LoginManager.ManagerType != Common.EnumType.ManagerType.Super) model.ManagerType = BLL.LoginUser.LoginManager.ManagerType;
            var ret = new BLL.Manager().Update(model);
            return UI.Utils.ToJsonString(ret);
        }

        /// <summary>
        /// 下线用户
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [UI.ValidateAuthority(ProgrameCode , "kickout")]
        public string KickOut(string userName)
        {
            Common.Result ret = new Common.Result(false, 0, "无用户下线");
            foreach (var u in userName.Split(','))
            {
                ret = BLL.LoginUser.RemoveOnlineManager(u);
            }
            if (userName.IndexOf(BLL.LoginUser.LoginManager.UserName)>=0)
            {
                var action = UI.LoginValidateAttribute.ValidLogin(HttpContext);
                if (action != null)
                {
                    action.ExecuteResult(this.ControllerContext);
                    return null;
                }
            }
            return UI.Utils.ToJsonString(ret);
        }
    }
}
